package com.wn.naturalbiorhythms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.wn.naturalbiorhythms.bean.NaturalBirthdayAdapter;
import com.wn.naturalbiorhythms.tool.BirthdaySettingsSharePref;

public class MoreOpActivity extends Activity {

	private ListView moreOpListView1;
	private NaturalBirthdayAdapter adapter1;
	private String[]showDays = {"17","23","28","33"};
	private byte showingDaysId;
	private String[]movetostr;
	private byte moveToDayId;
	private BirthdaySettingsSharePref bsspref;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//自定义title
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		
		setContentView(R.layout.more);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.activity_title);
		
		initTitleBar();
		
		initView();
		initData();
		setListener();
		NaturalBiorhythmsApplication.addActivity(this);
	}
	
	private void initTitleBar() {
		Button btn_back = (Button)findViewById(R.id.button1_back);
		btn_back.setText(R.string.title_bar_btn1_back);
		TextView titleTxt =  (TextView)findViewById(R.id.textView1_title);
		titleTxt.setText(R.string.title_bar_btn2_other);
		Button btn_other = (Button)findViewById(R.id.button2_other);
		btn_other.setText(R.string.title_bar_btn2_go2);
		btn_other.setEnabled(false);
		btn_other.setVisibility(View.INVISIBLE);
		
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
	}

	private void initView() {
		moreOpListView1 = (ListView)findViewById(R.id.ListView1_more_op);
		bsspref = BirthdaySettingsSharePref.getInstance(this); 
	}

	private void initData() {
		movetostr =new String[] {getString(R.string.menu_moveto_birthday_title),getString(R.string.menu_moveto_today_title),getString(R.string.menu_moveto_setting_title)};
		
		showingDaysId = bsspref.getShowingDaysId();
		moveToDayId = bsspref.getMoveToDayId();
		
		List<Map<String, Object>> dataList1 = new ArrayList<Map<String,Object>>();
		//
		Map<String, Object> map1_setting_birthday = new HashMap<String, Object>();
		map1_setting_birthday.put(NaturalBirthdayAdapter.key_title, getString(R.string.menu_setting_birthay_title));
		map1_setting_birthday.put(NaturalBirthdayAdapter.key_content, getString(R.string.desc_text1_setting_brithday));
		dataList1.add(map1_setting_birthday);
		
		//
		/*Map<String, Object> map2_setting_showdays = new HashMap<String, Object>();
		map2_setting_showdays.put(NaturalBirthdayAdapter.key_title, getString(R.string.menu_setting_showdays_title)+"("+showDays[showingDaysId]+")");
		map2_setting_showdays.put(NaturalBirthdayAdapter.key_content, getString(R.string.desc_text2_show_days));
		dataList1.add(map2_setting_showdays);*/
		
		//
		/*Map<String, Object> map3_moveto = new HashMap<String, Object>();
		map3_moveto.put(NaturalBirthdayAdapter.key_title, getString(R.string.menu_moveto_title)+"("+movetostr[moveToDayId]+")");
		map3_moveto.put(NaturalBirthdayAdapter.key_content, getString(R.string.desc_text3_moveto));
		dataList1.add(map3_moveto);*/
		
		//
		Map<String, Object> map4_to_know = new HashMap<String, Object>();
		map4_to_know.put(NaturalBirthdayAdapter.key_title, getString(R.string.menu_knowledge_title));
		map4_to_know.put(NaturalBirthdayAdapter.key_content, getString(R.string.desc_text4_toknow));
		dataList1.add(map4_to_know);
		
		//
		Map<String, Object> map5_helping = new HashMap<String, Object>();
		map5_helping.put(NaturalBirthdayAdapter.key_title, getString(R.string.menu_helping_title));
		map5_helping.put(NaturalBirthdayAdapter.key_content, getString(R.string.desc_text5_helping));
		dataList1.add(map5_helping);
		
		//
		Map<String, Object> map6_double2 = new HashMap<String, Object>();
		map6_double2.put(NaturalBirthdayAdapter.key_title, getString(R.string.menu_double2_model_title));
		map6_double2.put(NaturalBirthdayAdapter.key_content, getString(R.string.desc_text6_double2model));
		dataList1.add(map6_double2);
		
		adapter1 = new NaturalBirthdayAdapter(this,dataList1);
		moreOpListView1.setAdapter(adapter1);
		
	}

	private void setListener() {
		
		moreOpListView1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch(position){
				case 0:
					Intent intent0 = new Intent(MoreOpActivity.this, BirthdaySettingsActivity.class);
					startActivity(intent0);
					break;
				/*case 1:
					showingDaysId = bsspref.getShowingDaysId();
					AlertDialog.Builder showingdays = new AlertDialog.Builder(MoreOpActivity.this);
					showingdays.setTitle(R.string.menu_setting_showdays_title).setSingleChoiceItems(showDays, showingDaysId, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							bsspref.saveShowingDays((byte)which);
							((Map<String, Object> )adapter1.getItem(1)).put(NaturalBirthdayAdapter.key_title,getString(R.string.menu_setting_showdays_title)+"("+showDays[showingDaysId]+")");
							adapter1.notifyDataSetChanged();
						}
					})
					.setPositiveButton(R.string.positivebutton_text, null).show();
					break;
				case 2:
					moveToDayId = bsspref.getMoveToDayId();
					AlertDialog.Builder moveto = new AlertDialog.Builder(MoreOpActivity.this);
					moveto.setTitle(R.string.menu_moveto_title).setSingleChoiceItems(movetostr, moveToDayId, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							bsspref.saveMoveTo((byte)which);
							
							((Map<String, Object> )adapter1.getItem(2)).put(NaturalBirthdayAdapter.key_title,getString(R.string.menu_moveto_title)+"("+movetostr[moveToDayId]+")");
							adapter1.notifyDataSetChanged();
						}
					})
					.setPositiveButton(R.string.positivebutton_text, null).show();
					break;*/
				case 1:
					AlertDialog.Builder knowledge = new AlertDialog.Builder(MoreOpActivity.this);
					knowledge.setTitle(R.string.knowledgedialog_title).setMessage(
							R.string.knowledge_msg).setPositiveButton(
							R.string.positivebutton_text, null).show();
					break;
				case 2:
					AlertDialog.Builder help = new AlertDialog.Builder(MoreOpActivity.this);
					help.setTitle(R.string.helpdialog_title).setMessage(getString(R.string.helpdialog_msg)+getString(R.string.soft_version)).setPositiveButton(
							R.string.positivebutton_text, null).show();
					break;
				case 3:
					AlertDialog.Builder double2 = new AlertDialog.Builder(MoreOpActivity.this);
					double2.setTitle(R.string.menu_double2_model_title).setMessage(R.string.detail_text_double2model).setPositiveButton(
							R.string.positivebutton_text, null).show();
					break;
				}
			}
			
		});
	}
}
